package com.googlecode.mylyn.core.util;

import java.util.Map;

import org.eclipse.mylyn.tasks.core.IRepositoryQuery;

import com.google.gdata.client.projecthosting.IssuesQuery;

/**
 * This class is responsible for the mapping between {@link IRepositoryQuery}
 * and {@link IssuesQuery}.
 */
public final class QueryUtils {

    /** Full-text query string. */
    public static final String Q = "q";

    /** Canned query identifier. */
    public static final String CAN = "can";

    /** Issue ID. */
    public static final String ID = "id";

    /** Issue label. */
    public static final String LABEL = "label";

    /** Issue owner. */
    public static final String OWNER = "owner";

    /** Issue status. */
    public static final String STATUS = "status";

    private QueryUtils() {
        throw new AssertionError("not instantiable");
    }

    public static void copyAttributes(IRepositoryQuery mylynQuery, IssuesQuery googleQuery) {
        Map<String, String> attributes = mylynQuery.getAttributes();
        if (attributes.containsKey(Q)) {
            googleQuery.setFullTextQuery(attributes.get(Q));
        }
        if (attributes.containsKey(CAN)) {
            googleQuery.setCan(attributes.get(CAN));
        }
        if (attributes.containsKey(ID)) {
            googleQuery.setId(Integer.valueOf(attributes.get(ID)));
        }
        if (attributes.containsKey(LABEL)) {
            googleQuery.setLabel(attributes.get(LABEL));
        }
        if (attributes.containsKey(OWNER)) {
            googleQuery.setOwner(attributes.get(OWNER));
        }
        if (attributes.containsKey(STATUS)) {
            googleQuery.setOwner(attributes.get(STATUS));
        }
    }

    public static boolean isCanQuery(IRepositoryQuery query) {
        return query != null && query.getAttributes().containsKey(CAN);
    }

}
